<?php
/** 
 * item relation, adjust the detail on food and option realtion
 */
class item_relation_model extends CI_Model {
    private $error = array();
    private $is_valid_schema = false;
	/**
	 * constructor to load database object
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->database();
	}
    /**
     * @param $food_id
     * @return array
     */
    function get_item_relation($food_id)
    {
        $relations = array('add' => array(), 'remove' => array());
		$this->db->where('food_id', $food_id);
		$query = $this->db->get('food_option_item_relation');
		foreach ($query->result_array() as $row)
		{
            if($row['action'] === 'add'){
                $relations['add'][] = $row;
            }
            else if($row['action'] === 'remove'){
                $relations['remove'][] = $row;
            }
		}
		return $relations;
    }
    /**
     * @param $food_id
     * @param $food_option_id
     * @param $action
     * @return array
     * this method take care everything, include deletion of relation.
     */
    function create($food_id, $food_option_id, $action)
    {
        $this->error = array('user' => array(), 'system' => array());

        // prepare data
        $food_id = trim($food_id); $food_option_id = trim($food_option_id); $action = trim($action);

        // validate data
        $this->_validate_schema($food_id, $food_option_id, $action);
        if($this->is_valid_schema === true){
            $this->load->model('food_model', 'food');
            $this->load->model('food_option_model', 'food_option');
            $this->load->model('category_relation_model', 'category_relation');
            
            $food = $this->food->read($food_id);
            $food_category_relation = $this->category_relation->read($food['food_category_id']);
            $food_options = $this->food_option->get_option($food_category_relation['food_option_category_id']);

            $accpetable_action = "";
            if(array_key_exists($food_option_id, $food_options) === true){
                $accpetable_action = "remove";
            }
            else{
                $accpetable_action = "add";
            }

            if($accpetable_action !== $action){
                // remove relation since it is opposited to proper action, so it is reasonable to remove its relation
                $this->_delete($food_id, $food_option_id);
            }
            else{
                // good to create now
                $data = array(
                    'food_id' => $food_id,
                    'food_option_id' => $food_option_id,
                    'action' => $action
                );
                $this->db->insert('food_option_item_relation', $data);
            }
        }

        return $this->error;
    }
    /**
     * @param $food_id
     * @param $food_option_id
     * @return array
     */
    function _delete($food_id, $food_option_id){
        $this->error = array('user' => array(), 'system' => array());

        $this->db->where(array('food_id' => $food_id, 'food_option_id' => $food_option_id));
        $this->db->delete('food_option_item_relation');

        return $this->error;
    }
    /**
     * @param string $food_id
     * @param string $food_option_id
     * @param string $action
     * @return void
     */
    private function _validate_schema($food_id, $food_option_id, $action)
    {
        $this->is_valid_schema = false;

        $user_error_intro_count = count($this->error['user']);

        // validate schema

        $this->load->model('food_model', 'food');
        $this->load->model('food_option_model', 'food_option');

        $food = $this->food->read($food_id);
        $food_option = $this->food_option->read($food_option_id);

        if(count($food) === 0){
            $this->error['user']['food_id'] = "invalid food id";
        }

        if(count($food_option) === 0){
            $this->error['user']['food_option_id'] = "invalid food option id";
        }

        if('add' !== $action && 'remove' !== $action)
        {
            $this->error['user']['action_string'] = "invalid action, only accept add or remove";
        }

        $user_error_outro_count = count($this->error['user']);

        if($user_error_intro_count === $user_error_outro_count){
            $this->is_valid_schema = true;
        }
    }
}

/* End of file item_relation_model.php */
/* Location: ./system/application/models/item_relation_model.php */
